var app = getApp();
var custId = app.globalData.custID;
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typetext: "不限收货时间",
    buttonDisabled: false,
    modalHidden: true,
    show: false,
    list: [],
    selectProductId:0
  },
  showModal: function (e) {
    var pid = e.currentTarget.id;
    this.setData({
      modalHidden: !this.data.modalHidden,
      selectProductId: pid
    })
  },

  modalBindaconfirm: function () {
    this.setData({
      modalHidden: !this.data.modalHidden,
      show: !this.data.show,
      typetext: "不知道你选了什么",
      buttonDisabled: !this.data.buttonDisabled
    })
  },
  modalBindcancel: function () {
    this.setData({
      modalHidden: !this.data.modalHidden,
    })
  },
  //清空收藏
  delAll: function (e) {
    wx.showModal({
      title: '提示',
      content: '是否清空所有收藏？',
      showCancel: true,
      confirmText: "确定",
      success: function (res) {
        if(!res.confirm)
        {
          return false;
        }
        wx.showToast({
          title: '加载中...',
          icon: 'loading'
        })
        wx.request({
          url: app.globalData.apiurl +'/mobile/api/Collection/ClearMemberCollection',
          data: { custId: custId, userId: wx.getStorageSync('UserInfo').UserID },
          method: 'GET',
          success: function (res) {
            wx.hideToast();
            if (res.data.code == 1200) {
              var page = getCurrentPages().pop()
              if (page == undefined || page == null)
                return;
              page.onLoad()
            } else {
              wx.showModal({
                content: res.data.msg,
                showCancel: false,
                confirmText: "确定",
              })
            }
          }
        })
      }
    })
  },
  //删除
  del:function(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除收藏？',
      showCancel: true,
      confirmText: "确定",
      success: function (res) {
        if(!res.confirm)
        {
return false;
        }
        wx.showToast({
          title: '加载中...',
          icon: 'loading'
        })
        var data =
          {
            CustId: custId,
            UserId: wx.getStorageSync('UserInfo').UserID,
            ProductID: that.data.selectProductId,
          };
        wx.request({
          url: app.globalData.apiurl +'/mobile/api/Collection/CancelCollection',
          data: JSON.stringify(data),
          method: 'POST',
          success: function (res) {
            wx.hideToast();
            if (res.data.code == 1200) {
              that.setData({
                modalHidden: !that.data.modalHidden,
                selectProductId: 0
              })

              var page = getCurrentPages().pop()
              if (page == undefined || page == null)
                return;
              page.onLoad()
            } else {
              wx.showModal({
                content: res.data.msg,
                showCancel: false,
                confirmText: "确定",
              })
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit(function () {
    });
    loadCollect(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})

//加载数据
function loadCollect(that) {
  wx.showToast({
    title: '加载中...',
    icon: 'loading'
  })
  var data =
    {
      CustId: custId,
      UserId: wx.getStorageSync('UserInfo').UserID,
      PageIndex: 1,
      PageSize: 10
    };
  wx.request({
    url: app.globalData.apiurl +'/mobile/api/Collection/GetUnionMemberCollectionPage',
    data: JSON.stringify(data),
    method: 'POST',
    success: function (res) {
      wx.hideToast();
      if (res.data.code == 1200) {
        that.setData({
          list: res.data.content
        });
      } else {
        console.log('加载失败');
      }
    }
  })
}